<template>
  <div>
       <van-address-edit
      :area-list="areaList"
      :address-info="info"
      :show-delete="!!$route.query.id"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    >
      <div class="van-cell van-field">
        <div class="van-cell__title van-field__label"><span>标签</span></div>
        <div class="van-cell__value van-field__value">
          <div class="van-field__body">
            <van-radio-group v-model="label">
              <van-radio name="家" checked-color="#ff4400">家</van-radio>
              <van-radio name="公司" checked-color="#ff4400">公司</van-radio>
              <van-radio name="学校" checked-color="#ff4400">学校</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
    </van-address-edit>
  </div>
</template>

<script>
import {areaList} from '@vant/area-data';
import {reqaddressadd, reqaddresslist, reqaddressedit, reqaddressremove} from '../../http/http'
import { Toast } from 'vant';
export default {
  data(){
    return {
      areaList,
      label:'',
      info:{},//初始值
    }
  },
  mounted(){
    let id = this.$route.query.id;
    if(id){
      //编辑进来的
      reqaddresslist({
        uid:JSON.parse(localStorage.getItem("isLogin")).uid,
        id:id,
      }).then((res)=>{
        if(res.data.code ==200){
          let info = res.data.list[0]
          this. info = {
            id:info.id,
            name:info.username,
            tel:info.userphone,
            addressDetail:info.useraddress,
            areaCode:info.regioncode,
            isDefault:info.status == 1,
          };
          this.label = info.label
        }
      })
    }
  },
  methods:{
    onSave(e){
      if(this.$route.query.id){
        //修改
        reqaddressedit({
          id:e.id,
          username:e.name,
          userphone:e.tel,
          regioncode:e.areaCode,
          location:e.province + e.city+e.county,
          useraddress:e.addressDetail,
          status:e.isDefault?1:0,
          label:this.label,
        }).then((res)=>{
          if(res.data.code == 200){
            Toast(res.data.msg)
            this.$router.replace('/a-list')
          }
        })

      }else{
          reqaddressadd({
        uid:JSON.parse(localStorage.getItem("isLogin")).uid,
        username:e.name,
        userphone:e.tel,
        regioncode:e.areaCode,
        location:e.province + e.city+e.county,
        useraddress:e.addressDetail,
        status:e.isDefault?1:0,
        label:this.label,
      }).then((res)=>{
        if(res.data.code==200){
          Toast(res.data.msg)
          this.$router.replace('/a-list')
        }
      })

      }
    
    },
    onDelete(){
      //删除
      reqaddressremove({
        id:this.$route.query.id
      }).then(res=>{
        Toast(res.data.msg)
        this.$router.replace('/a-list')
      })
    }
  }

}
</script>

<style scoped lang="less">
@import "../../less/index.less";
.van-radio-group {
  display: flex;
  width: 100%;
}
.van-radio__icon,
.van-radio__icon .van-icon {
  display: inline-block !important;
}
.van-radio {
  margin-right: @margin20;
}
</style>